package com.share.config.wechat.pay;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * https://blog.csdn.net/m0_53434091/article/details/127801387
 * <p>
 * 支付逻辑参考
 */
@Slf4j
@Configuration
public class WxPayAutoConfiguration {
    @Resource
    private WxPayProperties properties;

    @Bean
    public WxPayService initWxPayService() {
        log.info("正在初始化微信Pay={}", JSON.toJSON(properties));

        //配置参数
        WxPayConfig config = new WxPayConfig();
        config.setAppId(properties.getAppid());
        config.setMchId(properties.getMchId());
        config.setMchKey(properties.getMchKey());
        config.setNotifyUrl(properties.getNotifyUrl());
        config.setKeyPath(properties.getKeyPath());
        config.setSignType(properties.getSignType());

        //创建实例
        WxPayService service = new WxPayServiceImpl();
        service.setConfig(config);
        return service;
    }
}
